<!--
 * @Author: Pure <305737824@qq.com>
 * @CreateTime: 2022-01-25 18:52:53
 * @LastEditors: Pure <305737824@qq.com>
 * @LastEditTime: 2022-02-15 13:43:49
 * @Description:多人协作
-->
<template>
  <el-form
    ref="form"
    label-position="right"
    label-width="128px"
    size="mini"
    :disabled="disabled"
    :model="formData"
  >
    <template v-for="(k, index) in formData.assist">
      <el-form-item :key="index" label-width="0px" class="form-item-wrapper">
        <el-col :span="9" class="no-padding">
          <el-form-item
            :ref="index+'user'"
            label="协作人"
            label-width="128px"
            :prop="'assist.'+index+'.name'"
            :rules="{ required: true, message: '请选择协作人', trigger: 'blur' }"
          >
            <!-- <el-input v-model="k.user" /> -->
            <el-input
              :ref="'sendUserInput'+index"
              v-model="k.name"
              style="width: auto"
              @focus="handleOpenAssistEngineerPanel(index)"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="协作事务"
            label-width="100px"
            :prop="'assist.'+index+'.content'"
            :rules="[{ required: true, message: '请输入协作事务' }]"
          >
            <el-input v-model="k.content" />

          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-button
            v-if="index!==0"
            style="margin-left:16px"
            @click="handleDeleteAssist(index)"
          >删除</el-button>
        </el-col>
      </el-form-item>
    </template>
    <el-form-item>
      <el-button
        @click="onAddAssist()"
      >添加协作</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import TicketApi from '@/feat/ticket/api';
import EngineerPanelSingle from '@/views/components/engineer/engineerPanel.vue';
import {
  mapGetters,
} from 'vuex';
export default {
  name: 'Assist',
  components: {

  },
  props: {
    disabled: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      formData: {
        assist: [{ user: '', content: '', name: null, engineerId: null, isTeam: false, teamId: null, }],
      },
    };
  },
  computed: {
    ...mapGetters(['userInfo']),
  },
  methods: {
    async onSubmit(ticketId) {
      await this.$refs.form.validate();
      const assist = this.formData.assist.map(item => {
        if (item.isTeam) {
          return {
            content: item.content,
            sendTeam: item.teamId, };
        } else {
          return {
            content: item.content,
            sendUser: item.engineerId, };
        }
      });
      await TicketApi.assist({
        ticketId,
        assist: JSON.stringify(assist),
      });
    },
    async handleOpenAssistEngineerPanel(index) {
      this.$refs['sendUserInput' + index][0].blur();
      this.currentAssistIndex = index;
      const defaultSelectedData = this.formData.assist[index];
      console.log(defaultSelectedData);
      this.$innerDrawer({
        width: '688px',
        component: () => <EngineerPanelSingle onDone={this.handleAssistSelectedEngineer} defaultSelectedData={defaultSelectedData} disabledEngineer={[this.userInfo.id]}/>,
      });
    },
    async handleAssistSelectedEngineer(data) {
      this.$refs['sendUserInput' + this.currentAssistIndex][0].blur();
      this.formData.assist[this.currentAssistIndex] = Object.assign(this.formData.assist[this.currentAssistIndex], data);
      this.$refs.form.validate();
    },
    onAddAssist() {
      this.formData.assist.push({
        user: '', content: '', name: null, engineerId: null, isTeam: false, teamId: null,
      });
    },
    handleDeleteAssist(index) {
      this.formData.assist.splice(index, 1);
    },
  },
};
</script>

<style lang="scss" scoped>
.form-item-wrapper {
  padding: 0;
  margin-bottom: 0;
  .el-col-12 {
    padding:0;
  }
}
</style>
